<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="摄像头名称：" prop="cameraName" :rules="[
      {
        required: true,
        message: '摄像头名称不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.cameraName"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="识别地址：" prop="pullUrl" :rules="[
      {
        required: true,
        message: '识别地址不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.pullUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="播放地址：" prop="pushUrl" :rules="[
      {
        required: true,
        message: '播放地址不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.pushUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="摄像头IP：" prop="cameraIp" :rules="[
      {
        required: true,
        message: '摄像头IP不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input v-model="form.cameraIp"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域：" prop="position" :rules="[
      {
        required: true,
        message: '请选择区域',
        trigger: 'change',
      },
    ]">
            <el-select :disabled="modeType === 1 ? false : true" v-model="form.zoneId" placeholder="请选择" >
              <el-option v-for="item in notRegion" :key="item.id" :value="item.id" :label="item.zoneName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存
                </el-button>
              </div>
            </div>


          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { addCamera, editCamera } from "@/api/camera";
import { getNotRegion } from "@/api/camera";

export default {
  props: {
    modeType: {
      type: Number,
      default: 1,
    },
    snapUserInfo: {
      type: Object,
      default: () => {
        return {};
      },
    }
  },
  mounted() {
    this.getNotRegion()
    if (this.modeType !== 1) {
      this.form = JSON.parse(JSON.stringify(this.snapUserInfo));
      console.log(this.form)
      this.form.isWarning = this.form.isWarning === 1 ? true : false;
    }
  },
  data() {
    return {
      notRegion: [],
      warnList: [
        {
          id: 0,
          name: "不预警",
        },
        {
          id: 1,
          name: "出现则预警",
        },
        {
          id: 2,
          name: "月末预警",
        },
      ],
      value1: 1,
      form: {
        id: 0,
        cameraName: "",
        isWarning: false,
        warningType: 1,
        traffic: 0,
        isCountEmployee: '0',
        pullUrl: '',
        cameraIp: null,
        zoneId: null,
        position: ''
      },
      loading: false,
    };
  },

  methods: {
    //区域
    async getNotRegion() {
      let res = await getNotRegion();
      if (res.code == 200) {
        this.notRegion = res.data;
      }
    },
    async addCamera() {
      this.notRegion.forEach((item)=>{
        if (item.id === this.form.zoneId){
          this.form.position = item.zoneName
        }
      })
      let res = await addCamera(this.form);

      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });

        this.loading = false;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.modeType == 1) {
            this.addCamera();
          } else {
            this.editCamera();
          }
        }
      });
    },
    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
    async editCamera() {
      await editCamera(this.form).then((res) => {
        if (res.code == 200) {
          this.$emit("onClose", {
            isClose: false,
          });
          this.$emit("onUpdate");
        } else {
          this.$message.error(res.msg)
        }

      });
    }
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .is-disabled {
  opacity: 0.4;
  background-color: rgba(23, 24, 26, 0.1);
}
</style>
